package gui;

import javax.swing.JPanel;

import Entities.ExecutionEntity;
import Entities.InspectionEntity;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JTextField;

public class DeniedExecutionsPanel extends JPanel implements ChatIF {
	
	private JLabel lblNewLabel;
	private JLabel lblDeniedExeutionNumber;
	private JButton btnViewExecution;
	private JTextArea textArea;
	private JButton btnAccept;
	private JButton btnBack;
	private AcademicFrame frame;
	private DeniedExecutionsPanel myself;
	private ExecutePanel executePanel;
	private JTextField textField;
	private String requestNum;
	private ArrayList<Object> listOfDenied;
	private String pid;
	private ViewRequestPanel viewRequest;
	private JComboBox<String> comboBox;
	



	public DeniedExecutionsPanel(AcademicFrame frame,ExecutePanel executePanel,String pid) 
	{
		this.myself=this;
		this.frame=frame;
		this.executePanel=executePanel;
		this.pid=pid;
		initialize();
	}
	
	
	
	public void initialize()
	{
		
		setLayout(null);
		
		lblNewLabel = new JLabel("Denied Executions");
		lblNewLabel.setFont(new Font("David", Font.BOLD, 20));
		lblNewLabel.setBounds(10, 11, 190, 36);
		add(lblNewLabel);
		
		lblDeniedExeutionNumber = new JLabel("Denied execution number:");
		lblDeniedExeutionNumber.setFont(new Font("David", Font.PLAIN, 15));
		lblDeniedExeutionNumber.setBounds(10, 71, 173, 14);
		add(lblDeniedExeutionNumber);
		
		btnViewExecution = new JButton("ViewExecution");
		btnViewExecution.setEnabled(false);
		btnViewExecution.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if (requestNum.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				else{
				btnViewExecution.setEnabled(true);
				viewRequest = new ViewRequestPanel(requestNum,frame,myself,false,"");
				frame.setSize(550, 780);
				frame.setContentPane(viewRequest);
				}
			}
		});
		btnViewExecution.setBounds(287, 243, 101, 23);
		add(btnViewExecution);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 111, 378, 126);
		add(panel);
		panel.setLayout(null);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setBounds(0, 0, 378, 126);
		panel.add(textArea);
		
		btnAccept = new JButton("Accept");
		btnAccept.setEnabled(false);
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
				ArrayList<Object> message = new ArrayList<Object>();
				String code = "changeStatusAndDel";
				String newStatus ="10";
				message.add(code);
				message.add(requestNum);
				message.add(newStatus);
				AcademicFrame.myclient.sendMessageToServer(message);
				frame.setSize(500,446);
				frame.setContentPane(executePanel);
			}
		});
		btnAccept.setBounds(246, 277, 89, 23);
		add(btnAccept);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 499);
				frame.setContentPane(executePanel);
			}
		});
		btnBack.setBounds(85, 277, 89, 23);
		add(btnBack);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(124, 311, 86, 20);
		add(textField);
		textField.setColumns(10);
		textField.setText(pid);
		JLabel lblExecutionid = new JLabel("ExecutionLeaderID:");
		lblExecutionid.setBounds(10, 314, 121, 14);
		add(lblExecutionid);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				requestNum = (String)comboBox.getSelectedItem();
				textArea.setText("");
				if(!requestNum.equalsIgnoreCase("--Choose a request--"))
				{
					
					btnViewExecution.setEnabled(true);
					btnAccept.setEnabled(true);
					//int num = (int)listOfDenied.get(1);
					int num = Integer.parseInt(listOfDenied.get(1).toString());
					int i;
					for (i=0;i<num;i++)
					{
						InspectionEntity en = new InspectionEntity();
						en = (InspectionEntity) listOfDenied.get(i+2);
						if(en.getInspectionID().equals(requestNum))
							textArea.setText(en.getFaults());
					}
					
				}
				
			}
		});
		comboBox.setBounds(193, 68, 171, 20);
		add(comboBox);
	}
	
	

	@Override
	public void display(Object message)
	{

		listOfDenied=new ArrayList<Object>();
		int i;
		ArrayList<Object> exReturn=new ArrayList<Object>();
		listOfDenied.clear(); // clear the info array list
		
		listOfDenied = (ArrayList<Object>)message;
		InspectionEntity ee = new InspectionEntity();
		
		//TaskDuration = (ArrayList<Object>)message;
	
		if (((String)(listOfDenied.get(0))).equalsIgnoreCase("getDeniedExecutions"))
		{
			//ArrayList<String> deniedExecutions = (ArrayList<String>)message; 				
			//int num = (int) listOfDenied.get(1);
			int num = Integer.parseInt(listOfDenied.get(1).toString());
			this.comboBox.addItem("--Choose a Request--");
			for (i=0; i < num ; i++ )
			{
				ee = (InspectionEntity) listOfDenied.get(i+2);
				this.comboBox.addItem(ee.getInspectionID());
			}
		
		}
		else if(((String)(listOfDenied.get(0))).equalsIgnoreCase("changeStatusAndDel"))
		{
			if(((String)(listOfDenied.get(1))).equalsIgnoreCase("Valid"))
			{
				(new Dialog("The Denied Execution have returned to the Execution phase")).setVisible(true);
			}
			else {
				
			}
		}
		
	}
}
